package com.portal.service.impl.system;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.portal.model.system.FunInfo;
import com.portal.model.system.Menu;
import com.portal.model.system.Role;
import com.portal.model.system.RolePower;
import com.portal.service.impl.BaseService;
import com.portal.service.system.RoleService;
import com.util.portal.PageConfig;

public class RoleServiceImpl extends BaseService implements RoleService {

	public Integer getRowCount(Role role) {
		return this.getRoleDao().getRowCount(role);
	}

	public Role read(String id) {
		return this.getRoleDao().read(id);
	}

	public List<Role> getList(Role role, PageConfig pageConfig) {
		if (pageConfig != null) {
			Integer rowcount = this.getRoleDao().getRowCount(role);
			pageConfig.setTotalCount(rowcount);
			return this.getRoleDao().getList(role,
					this.getRowBounds(pageConfig));
		} else {
			return this.getRoleDao().getList(role);
		}
	}

	public JSONArray getRolePowerMenuTree(String roleCode) {
		List<Menu> listMenu1 = this.getSysMenuDao().getRolePowerMenuTree(roleCode,"1");
		List<Menu> listMenu2 = this.getSysMenuDao().getRolePowerMenuTree(roleCode,"2");
		List<FunInfo> listFunInfo1 = this.getFunInfoDao().getRolePowerFunInfoTree(roleCode, "1");
		List<FunInfo> listFunInfo2 = this.getFunInfoDao().getRolePowerFunInfoTree(roleCode, "2");
		
		//一层菜单
		JSONArray jsarry = new JSONArray();
		for (Menu menu1 : listMenu1) {
			JSONObject jo1 = new JSONObject();
			jo1.put("id", menu1.getMenuCode());
			jo1.put("text", menu1.getMenuName());
			
			//二层菜单
			int menu2Num=0;
			JSONArray jsarry2 = new JSONArray();
			for (Menu menu2 : listMenu2) {
				if(menu2.getParentMenuCode().equals(menu1.getMenuCode())){
					JSONObject jo2 = new JSONObject();
					jo2.put("id", menu2.getMenuCode());
					jo2.put("text", menu2.getMenuName());
					menu2Num++;
					
					//按钮级
					int subFunInfo1Num=0;
					JSONArray jsarry3 = new JSONArray();
					for (FunInfo funInfo1 : listFunInfo1) {
						JSONObject jo3 = new JSONObject();
						if(funInfo1.getMenuCode().equals(menu2.getMenuCode())){
							jo3.put("id", menu2.getMenuCode()+"#"+funInfo1.getFunCode());
							jo3.put("text", funInfo1.getFunNameCn());
							subFunInfo1Num++;
							
							//按钮菜单级
							int subFunInfo2Num=0;
							JSONArray jsarry4 = new JSONArray();
							for (FunInfo funInfo2 : listFunInfo2) {
								JSONObject jo4 = new JSONObject();
								if(funInfo2.getParentFun().equals(funInfo1.getFunCode())){
									jo4.put("id", funInfo1.getMenuCode()+"#"+funInfo2.getFunCode());
									jo4.put("text", funInfo2.getFunNameCn());
									jo4.put("checked", funInfo2.getIsValid().equals("1")?true:false); //叶子节点增加checked属性
									subFunInfo2Num++;
									jsarry4.add(jo4);
								}
							}
							
							if(subFunInfo2Num>0){
								jo3.put("children", jsarry4);
							}else{
								jo3.put("checked", funInfo1.getIsValid().equals("1")?true:false); //没有叶子节点，且已选中保存
							}
							jsarry3.add(jo3);
						}
					}
					if(subFunInfo1Num>0){
						jo2.put("children", jsarry3);
					}else{
						jo2.put("checked", menu2.getIsValid().equals("1")?true:false); //没有叶子节点，且已选中保存
					}
					jsarry2.add(jo2);
				}
			}
			if(menu2Num>0){
				jo1.put("children", jsarry2);
			}else{
				jo1.put("checked", menu1.getIsValid().equals("1")?true:false); //没有叶子节点，且已选中保存
			}
			jsarry.add(jo1);
		}
		
		System.out.println("============="+jsarry.toString());
		
		return jsarry;
	}

	public void setRolePower(String roleCode ,String menuCodes,String funCodes) {
		
		String menuCode[] = menuCodes.split(",");
		String funCode[] = funCodes.split(",");
		
		RolePower allRolePower = new RolePower();
		this.getRolePowerDao().delete(allRolePower);
		
		//循环写入菜单项
		for(int i=0;i<menuCode.length;i++){
			String menuId = menuCode[i];
			
			RolePower rolePower = new RolePower();
			rolePower.setRoleCode(roleCode);
			rolePower.setMenuCode(menuId);
			this.getRolePowerDao().insert(rolePower);
		}
		
		//循环写入按钮项
		for(int i=0;i<funCode.length;i++){
			String funParentMenuId = funCode[i].split("#")[0];
			String funId = funCode[i].split("#")[1];
			
			RolePower rolePower = new RolePower();
			rolePower.setRoleCode(roleCode);
			rolePower.setMenuCode(funParentMenuId);
			rolePower.setFunCode(funId);
			this.getRolePowerDao().insert(rolePower);
		}
	}

	public void deleteRolePower(String roleCode, String menuCode, String funCode) {
		RolePower rolePower = new RolePower();
		rolePower.setRoleCode(roleCode);
		rolePower.setMenuCode(menuCode);
		rolePower.setFunCode(funCode);
		this.getRolePowerDao().delete(rolePower);
	}
}
